const fs = require('fs')
const xmlbuild = require('xmlbuilder')
const lodash = require('lodash')
const rq = require('inquirer')
const os = require('os')
const path = require('path')
const xml2j = require('xml-js')

const addonPath = path.join(os.homedir(), 'AppData/Roaming/kingsoft/wps/jsaddons')
const publishXmlPath = path.join(addonPath, 'publish.xml')
const jsPluginXmlPath = path.join(addonPath, 'jsplugins.xml')
const backupFilePath = path.join(addonPath, 'backup_publish.xml')

function getXML(xml) {
    let res;
    try {
        let foo = fs.readFileSync(xml, "utf-8")
        res = xml2j.xml2json(foo, { compact: true })
    } catch {
        throw Error("   没有检测到改文件!")
    }
    return res;
}
function getJSpluginLocal() {
    let fo = getXML(jsPluginXmlPath)
    let foo = JSON.parse(fo);
    let online, offline;
    try {
        online = lodash.isArray(foo.jsplugins.jspluginonline) ? lodash.map(foo.jsplugins.jspluginonline, o => o._attributes) : foo.jsplugins.jspluginonline._attributes;
    } catch {
        online = null;
    }
    try {
        offline = lodash.isArray(foo.jsplugins.jspluginoffline) ? lodash.map(foo.jsplugins.jspluginoffline, o => o._attributes) : foo.jsplugins.jspluginoffline._attributes;
    } catch {
        offline = null;
    }
    return {
        online: online,
        offline: offline
    }
}
function getPublishPlugins() {
    let ps = getXML(publishXmlPath);
    let ps_enable,
        ps_disEnable = [];
    let foo = JSON.parse(ps)
    let plugins = foo.jsplugins.jsplugin;
    let isarr = lodash.isArray(plugins);
    if (isarr) {
        ps_enable = lodash.filter(plugins, o => {
            let isEnable = o._attributes.enable;
            if (["true", "enable_dev"].includes(isEnable)) {
                return o._attributes;
            } else {
                ps_disEnable.push(o)
            }
        })
    } else {
        let p = foo.jsplugins.jsplugin._attributes;
        if (["true", "enable_dev"].includes(p.enable)) {
            ps_enable = p;
        } else {
            ps_disEnable = p;
        }
    }

    return {
        enables: lodash.isArray(ps_enable) ? lodash.map(ps_enable, o => o._attributes) : ps_enable,
        disables: lodash.isArray(ps_disEnable) ? lodash.map(ps_disEnable, o => o._attributes) : ps_disEnable
    }
}
function allPublishPlugins() {
    let foo = lodash.concat(getPublishPlugins().enables, getPublishPlugins().disables)
    return foo;
}
function updateObject(option = { name: "", enable: "" }) {
    let appconfs = allPublishPlugins()
    let res = lodash.map(appconfs, o => {
        if (o.name == option.name) {
            o = lodash.assignIn(o, { enable: option.enable })
            return o;
        } else {
            return o;
        }
    })
    return res;
}
function buildXML(obj) {
    let objs = obj;
    let root = xmlbuild.create('jsplugins', { encoding: "UTF-8" })
    for (var i = 0; i < objs.length; i++) {
        let item_obj = objs[i];
        let len_item_obj_keys = lodash.keys(item_obj)
        var item = root.ele('jsplugin');
        for (let z = 0; z < len_item_obj_keys.length; z++) {
            let ztem = len_item_obj_keys[z];
            item.att(ztem, item_obj[ztem]);
        }
    }
    var xml = root.end({ pretty: true });
    return xml.toString()
}

function changePublishxml(xml) {
    // 备份原文件
    if (fs.existsSync(backupFilePath) == false) {
        fs.copyFileSync(publishXmlPath, backupFilePath);
    }
    // 更改
    fs.writeFileSync(publishXmlPath, xml, { encoding: 'utf-8' })
}
module.exports = {
    publishXmlPath,
    jsPluginXmlPath,
    getXML,
    getPublishPlugins,
    updateObject,
    buildXML,
    changePublishxml,
    getJSpluginLocal,
    allPublishPlugins,
}